package com.git.hui.yinshua.biz.rest.common;

import com.git.hui.yinshua.api.model.ResVo;
import com.git.hui.yinshua.api.model.permission.Permission;
import com.git.hui.yinshua.api.model.permission.UserAccessRole;
import com.git.hui.yinshua.biz.common.service.ImageService;
import com.git.hui.yinshua.biz.rest.models.img.ImgTokenDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author YiHui
 * @date 2024/6/14
 */
@RestController
@RequestMapping(path = "img")
@Permission(role = UserAccessRole.LOGIN)
public class ImgUploadController {
    @Autowired
    private ImageService imageService;

    /**
     * 图片上传
     *
     * @param request
     * @return
     */
    @PostMapping(path = "upload")
    public ResVo<String> upload(HttpServletRequest request) {
        String img = imageService.saveImg(request);
        return ResVo.ok(img);
    }

    /**
     * 获取七牛上传的token
     *
     * @return
     */
    @GetMapping(path = "token")
    public ResVo<ImgTokenDto> accessToken() {
        return ResVo.ok(imageService.uploadToken());
    }
}
